package impl.ui;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import classes.Constants;
import classes.Coordinate;

/**
 * Tile of the map view. Color in function of state
 */
public class MapTile {
	public static final int PADDING = 5;
	
	public static final int STATE_DEFAULT = 0;
	
	public static final int STATE_OBSTACLE = 1;
	
	public static final int STATE_CANDY = 2;
	
	public static final int STATE_ROBOT = 3;
	
	public static final int STATE_ROBOT_WITH_CANDY = 4;
	
	public static final int STATE_CANDY_BGR = 5;
	
	public static final int STATE_ROBOT_BGR = 6;
	
	private Rectangle2D.Double rect;
	
	private Coordinate position;
	
	private int state = STATE_DEFAULT;
	
	private Color currentColor;
	
	private Color[] colors = new Color[7];
	
	public MapTile(Rectangle2D.Double rect, Coordinate position, int state) {
		this.rect = rect;
		this.position = position;
		this.state = state;
		
		initColors();
		
		currentColor = colors[0];
	}
	
	/**
	 * Draws the current tile in it's rectangle
	 * @param g2 The {@link Graphics2D} element of the parent view's layout
	 */
	public void draw(Graphics2D g2) {
		g2.setPaint(currentColor);
		g2.fill(rect);
		g2.setPaint(colors[0]);
		g2.draw(rect);
	}
	
	/**
	 * Sets the current state of the tile, as one of the static values defined in this class
	 * @param state	The new state. The color of the tile wil be set automatically.
	 */
	public void setState(int state) {
		if (state < 0 || state > 6)
			return;
		
		this.state = state;
		
		currentColor = colors[state];
	}
	
	/**
	 * Method for retrieving the current state of the tile
	 * @return	The current state of the tile
	 */
	public int getState() {
		return state;
	}
	
	/**
	 * Method for getting the tile's current map coordinates
	 * @return	The coordinates object
	 */
	public Coordinate getPosition() {
		return position;
	}
	
	/**
	 * Initialize default colors objects
	 */
	private void initColors() {
		colors[STATE_DEFAULT] = new Color(Constants.MAIN_BGR_COLOR);
		colors[STATE_OBSTACLE] = new Color(Constants.MAIN_OBSTACLE_COLOR);
		colors[STATE_CANDY] = new Color(Constants.MAIN_CANDY_COLOR);
		colors[STATE_CANDY_BGR] = new Color(Constants.MAIN_CANDY_BGR_COLOR);
		colors[STATE_ROBOT] = new Color(Constants.MAIN_ROBOT_COLOR);
		colors[STATE_ROBOT_BGR] = new Color(Constants.MAIN_ROBOT_BGR_COLOR);
		colors[STATE_ROBOT_WITH_CANDY] = new Color(Constants.MAIN_ROBOT_WITH_CANDY_COLOR);
	}
	
}
